﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for TinTucDAO
/// </summary>
public class TinTucDAO
{
    DataProvider provider;
	public TinTucDAO()
	{
        provider = new DataProvider();
	}
    public List<TinTuc> HienThiTinTucNoiBat()
    {
        List<TinTuc> list = new List<TinTuc>();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_HienThiTinTucNoiBat", provider.Conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            cmd.Connection.Close();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                TinTuc dto = new TinTuc();
                dto.IDTinTuc = Convert.ToInt32(ds.Tables[0].Rows[i]["IDTinTuc"].ToString());
                dto.TieuDe = ds.Tables[0].Rows[i]["TieuDe"].ToString();
                dto.NoiDung = ds.Tables[0].Rows[i]["NoiDung"].ToString();
                dto.HinhAnh = ds.Tables[0].Rows[i]["HinhAnh"].ToString();
                dto.NgayDang = Convert.ToDateTime(ds.Tables[0].Rows[i]["NgayDang"].ToString());
                dto.TenTat = ds.Tables[0].Rows[i]["TenTat"].ToString();
                list.Add(dto);
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return list;
    }

    public List<TinTuc> HienThiTinTuc()
    {
        List<TinTuc> list = new List<TinTuc>();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_HienThi", provider.Conn);
            cmd.Parameters.Add("@TenBang", SqlDbType.VarChar).Value = "TinTuc";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            cmd.Connection.Close();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                TinTuc dto = new TinTuc();
                dto.IDTinTuc = Convert.ToInt32(ds.Tables[0].Rows[i]["IDTinTuc"].ToString());
                dto.TieuDe = ds.Tables[0].Rows[i]["TieuDe"].ToString();
                dto.GioiThieu = ds.Tables[0].Rows[i]["GioiThieu"].ToString();
                dto.NoiDung = ds.Tables[0].Rows[i]["NoiDung"].ToString();
                dto.HinhAnh = ds.Tables[0].Rows[i]["HinhAnh"].ToString();
                dto.NgayDang = Convert.ToDateTime(ds.Tables[0].Rows[i]["NgayDang"].ToString());
                dto.TenTat = ds.Tables[0].Rows[i]["TenTat"].ToString();
                list.Add(dto);
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return list;
    }

    public List<TinTuc> HienThiTinTucMoi(int idTinTuc)
    {
        List<TinTuc> list = new List<TinTuc>();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_HienThiTinTucMoi", provider.Conn);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = idTinTuc;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            cmd.Connection.Close();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                TinTuc dto = new TinTuc();
                dto.IDTinTuc = Convert.ToInt32(ds.Tables[0].Rows[i]["IDTinTuc"].ToString());
                dto.TieuDe = ds.Tables[0].Rows[i]["TieuDe"].ToString();
                dto.GioiThieu = ds.Tables[0].Rows[i]["GioiThieu"].ToString();
                dto.NoiDung = ds.Tables[0].Rows[i]["NoiDung"].ToString();
                dto.HinhAnh = ds.Tables[0].Rows[i]["HinhAnh"].ToString();
                dto.NgayDang = Convert.ToDateTime(ds.Tables[0].Rows[i]["NgayDang"].ToString());
                dto.TenTat = ds.Tables[0].Rows[i]["TenTat"].ToString();
                list.Add(dto);
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return list;
    }

    public List<TinTuc> HienThiChiTietTinTuc(int idTinTuc)
    {
        List<TinTuc> list = new List<TinTuc>();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_HienThiChiTietTinTuc", provider.Conn);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = idTinTuc;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            cmd.Connection.Close();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                TinTuc dto = new TinTuc();
                dto.IDTinTuc = Convert.ToInt32(ds.Tables[0].Rows[i]["IDTinTuc"].ToString());
                dto.TieuDe = ds.Tables[0].Rows[i]["TieuDe"].ToString();
                dto.GioiThieu = ds.Tables[0].Rows[i]["GioiThieu"].ToString();
                dto.NoiDung = ds.Tables[0].Rows[i]["NoiDung"].ToString();
                dto.HinhAnh = ds.Tables[0].Rows[i]["HinhAnh"].ToString();
                dto.NgayDang = Convert.ToDateTime(ds.Tables[0].Rows[i]["NgayDang"].ToString());
                dto.TenTat = ds.Tables[0].Rows[i]["TenTat"].ToString();
                list.Add(dto);
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return list;
    }

    public int LayMaxIDTinTuc()
    {
        int kq = 0;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_MaxIDTinTuc", provider.Conn);

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 6));
            cmd.Parameters["@ID"].Direction = ParameterDirection.Output;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();           

            kq = int.Parse(cmd.Parameters["@ID"].Value.ToString());
            
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return kq;
    }

    public int ThemTinTuc(TinTuc tt)
    {
        int kq = 0;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_ThemTinTuc", provider.Conn);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = tt.IDTinTuc;
            cmd.Parameters.Add("@TieuDe", SqlDbType.NText).Value = tt.TieuDe;
            cmd.Parameters.Add("@GioiThieu", SqlDbType.NText).Value = tt.GioiThieu;
            cmd.Parameters.Add("@NoiDung", SqlDbType.NText).Value = tt.NoiDung;
            cmd.Parameters.Add("@HinhAnh", SqlDbType.VarChar).Value = tt.HinhAnh;
            cmd.Parameters.Add("@TenTat", SqlDbType.NText).Value = tt.TenTat;

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            kq = 1;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return kq;
    }

    public int CapNhatTinTuc(TinTuc tt)
    {
        int kq = 0;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_ThemTinTuc", provider.Conn);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = tt.IDTinTuc;
            cmd.Parameters.Add("@TieuDe", SqlDbType.NText).Value = tt.TieuDe;
            cmd.Parameters.Add("@GioiThieu", SqlDbType.NText).Value = tt.GioiThieu;
            cmd.Parameters.Add("@NoiDung", SqlDbType.NText).Value = tt.NoiDung;
            cmd.Parameters.Add("@HinhAnh", SqlDbType.VarChar).Value = tt.HinhAnh;
            cmd.Parameters.Add("@TenTat", SqlDbType.NText).Value = tt.TenTat;

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            kq = 1;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return kq;
    }


    public int XoaTinTuc(int id)
    {
        int kq = 0;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_XoaTinTuc", provider.Conn);
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = id;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            kq = 1;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            provider.Conn.Close();
        }
        return kq;
    }
}